package main

import (
	"fmt"
	"jnjword/models"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

type Product struct {
	gorm.Model
	Code  string
	Price uint
}

func main() {
	db, err := gorm.Open(mysql.Open("root:Zxw.951212@tcp(43.138.27.18:3306)/user_basic?charset=utf8mb4&parseTime=True&loc=Local"), &gorm.Config{})
	if err != nil {
		panic("failed to connect database")
	}

	// 自动创建表 添加字段   如果user表新增了字段 使用这个即可
	db.AutoMigrate(&models.UserBasic{})
	db.AutoMigrate(&models.WordBasic{})

	// Create
	user := &models.UserBasic{}
	user.Name = "张三"
	user.Password = "123456"
	db.Create(user)

	// Read

	// 打印用户
	fmt.Println(db.First(&user, 2))
	//db.First(&user, "code = ?", "D42") // find product with code D42

	// Update - update product's price to 200
	db.Model(user).Update("password", "123456")
	// Update - update multiple fields
	// db.Model(&product).Updates(Product{Price: 200, Code: "F42"}) // non-zero fields
	// db.Model(&product).Updates(map[string]interface{}{"Price": 200, "Code": "F42"})

	// Delete - delete product
	// db.Delete(&user, 1)
}
